function initArtTable() {
    $.ajax({
        url: "/my/article/cates",
        success: (res) => {
            var htmlStr = template("tpl-table", res)
            $("tbody").html(htmlStr)
        }
    })
}

$(function () {
    initArtTable()
    // var indexAdd = null
    $("#btnAddCate").on("click", function () {
        indexAdd = layui.layer.open({
            type: 1,
            area: ["500px", "250px"],
            title: '添加文章分类',
            content: $("#AddArticle").html()
        })
    })
    //添加
    $("body").on("submit", "#formAdd", function (e) {
        e.preventDefault()
        $.ajax({
            type: "POST",
            url: "/my/article/addcates",
            data: $(this).serialize(),
            success: (res) => {
                if (res.status !== 0) {
                    return layui.layer.msg("添加失败")
                }
                layui.layer.msg("添加成功")
                layui.layer.close(indexAdd)
                initArtTable()
            }
        })
    })
    //编辑获取数据框
    $("tbody").on("click", "#btn-edit", function (e) {
        indexedit = layui.layer.open({
            type: 1,
            area: ["500px", "250px"],
            title: '修改文章分类',
            content: $("#editArticle").html()
        })
        var id = $(this).attr("data-id")

        // console.log(id);
        $.ajax({
            url: "/my/article/cates/" + id,
            success: (res) => {
                console.log(res);
                if (res.status == 0) {
                    layui.form.val("formEdit", res.data)
                }
            }
        })
    })
    //编辑修改
    $("body").on("submit", "#formEdit", function (e) {
        e.preventDefault()
        $.ajax({
            type: "POST",
            url: "/my/article/updatecate",
            data: $(this).serialize(),
            success: (res) => {
                if (res.status !== 0) {
                    return layui.layer.msg("修改失败")
                }
                layui.layer.msg("修改成功")
                layui.layer.close(indexedit)
                initArtTable()
            }
        })
    })
    //删除
    $("tbody").on("click", "#del", function (e) {

        var id = $(this).data('id')
        layer.confirm('确定要删除么', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //do something
            $.ajax({
                type: "get",
                url: "/my/article/deletecate/" + id,
                success: function (res) {
                    // console.log(res);
                    if (res.status !== 0) {
                        return layui.layer.msg("删除失败")
                    }
                    layui.layer.msg("删除成功")
                    layer.close(index);
                    initArtTable()
                }
            })
        });
    })
})


// $.ajax({
//     url: "/my/article/deletecate/" + id,
//     success: function (res) {
//         // console.log(res);
//         if (res.status !== 0) {
//             return layui.layer.msg("删除失败")
//         }
//         layui.layer.msg("删除成功")
//         initArtTable()
//     }
// })